<?php

namespace application\admin\controller;

use ticky\page;

class logs extends base {

    private $logdir = RUNTIME_PATH . './log/';

    public function index() {
        $operation = \ticky\request::get('operation', 'error');
        $p = \ticky\request::get('p', '0');

        $logfiles = $this->get_log_files($this->logdir, $operation);
        $logs = array();
        $lastkey = count($logfiles) - 1;
        $lastlog = !empty($logfiles) ? $logfiles[$lastkey] : '';
        krsort($logfiles);
        if ($logfiles) {
            if (!isset($_GET['day']) || strexists($_GET['day'], '_')) {
                $filename = $this->logdir . $lastlog;
                $logs = file($filename);
            } else {
                $filename = $this->logdir . $_GET['day'] . '_' . ($operation == 'sendmail' ? '' : 'error') . '.php';
                $logs = file($filename);
            }
        }
        $total = count($logs);
        $start = ($p - 1) * 20;
        $page = new page($total, $start);
        $logs = array_reverse($logs);
        $logs = array_slice($logs, $start, 20);

        if ($operation == 'error') {
            $data = array();
            $str = "";
            foreach ($logs as $logrow) {
                $log = explode("\t", $logrow);
                $str .= "<tr><td>" . $log[0] . "</td><td>" . $log[1] . "</td><td>" . $log[2] . "</td><td>" . $log[3] . "</td><td>" . $log[4] . "</td></tr>";
                $data[] = $log;
            }
            $htmlcontext = "<tr class='header'><th>日期</th><th>IP</th><th>用户</th><th>路径</th><th>内容</th></tr>" . $str;
            $this->assign('html', $htmlcontext);
        } else if ($operation == 'sendmail') {

        }

        $this->assign('operation', $operation);
        $this->assign('page', $page);
        $this->display('logs/index');
    }

    private function get_log_files($logdir = '', $action = 'error') {
        $dir = opendir($logdir);
        $files = array();
        while ($entry = readdir($dir)) {
            if ($entry != '.' && $entry != '..') {
                $files[] = $entry;
            }
        }
        closedir($dir);
        if ($files) {
            sort($files);
            $logfile = $action;
            $logfiles = array();
            $ym = '';
            foreach ($files as $file) {
                if (strpos($file, $logfile) !== FALSE) {
                    if (substr($file, 0, 6) != $ym) {
                        $ym = substr($file, 0, 6);
                    }
                    $logfiles[$ym][] = $file;
                }
            }
            if ($logfiles) {
                $lfs = array();
                foreach ($logfiles as $ym => $lf) {
                    $lastlogfile = $lf[0];
                    unset($lf[0]);
                    $lf[] = $lastlogfile;
                    $lfs = array_merge($lfs, $lf);
                }
                return $lfs;
            }
            return array();
        }
        return array();
    }

}
